var t = require("../../../@babel/runtime/helpers/slicedToArray"),
  e = require("../../../utils/index"),
  n = e.operateDom.requestAnimationFrame,
  r = e.operateDom.getAllRect,
  a = e.operateDom.getRect,
  s = e.operateDom.nextTick,
  o = e.tools.common;
Component({
  externalClasses: ["tab-extend-class"],
  properties: {
    lineWidth: {
      type: Number,
      value: 30
    },
    tabList: {
      type: Array,
      value: []
    },
    duration: {
      type: Number,
      value: .3
    },
    tabHeight: {
      type: String,
      value: "88rpx"
    },
    isEqualityAlign: {
      type: Boolean,
      value: !0
    },
    active: {
      type: null,
      value: 0,
      observer: function (t) {
        t !== this.data.currentIndex && this.setCurrentIndex(t)
      }
    }
  },
  data: {
    currentIndex: 0,
    scrollLeft: 0,
    lineOffsetLeft: 0,
    skipTransition: !0
  },
  lifetimes: {
    attached: function () {
      this.firstFlag = !0
    }
  },
  ready: function () {
    var t = this;
    this.firstFlag = !1, n((function () {
      var e;
      null !== (e = t.data.tabList) && void 0 !== e && e.length && (t.resize(), t.scrollIntoView())
    }))
  },
  methods: {
    onTap: function (t) {
      var e = t.currentTarget.dataset.index;
      this.setCurrentIndex(e)
    },
    setCurrentIndex: function (t) {
      var e = this,
        i = this.data;
      !o.isDef(t) || t >= i.tabList.length || t < 0 || t === i.currentIndex || (this.setData({
        currentIndex: t
      }), n((function () {
        e.resize(), e.scrollIntoView()
      })), s((function () {
        var t = e.data,
          i = t.currentIndex,
          n = t.tabList;
        e.firstFlag || e.triggerEvent("change", {
          row: n[i],
          index: i
        })
      })))
    },
    resize: function () {
      var e = this,
        i = this.data,
        n = i.currentIndex,
        o = i.skipTransition;
      Promise.all([r(this, ".tab-item"), a(this, ".tabs_line")]).then((function (i) {
        var r = t(i, 2),
          a = r[0],
          l = void 0 === a ? [] : a,
          u = r[1],
          c = l[n];
        if (null != c) {
          var d = l.slice(0, n).reduce((function (t, e) {
            return t + e.width
          }), 0);
          d += (c.width - u.width) / 2, e.setData({
            lineOffsetLeft: d
          }), o && s((function () {
            e.setData({
              skipTransition: !1
            })
          }))
        }
      }))
    },
    scrollIntoView: function () {
      var e = this,
        i = this.data.currentIndex;
      Promise.all([r(this, ".tab-item"), a(this, ".tab-wrap")]).then((function (n) {
        var r = t(n, 2),
          a = r[0],
          s = r[1],
          o = a[i],
          l = a.slice(0, i).reduce((function (t, e) {
            return t + e.width
          }), 0);
        e.setData({
          scrollLeft: l - (s.width - o.width) / 2
        })
      }))
    }
  }
});